找传奇、传世资源到传世资源站!

C# 实现飞机大战 游戏源码(可以直接运行)

8.5玩家评分(1人评分)
下载后可评
介绍 评论 失效链接反馈

windows应用from clipboard from clipboard

using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Threading;using System.Threading.Tasks;using System.Windows.Forms;namespace GameAir{ public partial class gameconser : Form { //游戏准备结束的imge public Image imgGamePrepare = Image.FromFile("..\\..\\image\\gamestart.jpg"); public Image imgGameOver = Image.FromFile("..\\..\\image\\gameover.jpg"); //背景的image public Image imgBackGround = Image.FromFile("..\\..\\image\\back1.png"); //我军飞机的image public Image imgMyPlane = Image.FromFile(@"..\..\image\plane.png"); public Image imgMyPlaneLeft = Image.FromFile(@"..\..\image\left.png"); public Image imgMyPlaneRight = Image.FromFile(@"..\..\image\right.png"); //敌机的image public Image imgEnemyPlane1 = Image.FromFile(@"..\..\image\enemyplane1.png"); public Image imgEnemyPlane2 = Image.FromFile(@"..\..\image\enemyplane2.png"); //子弹的image public Image imgBullet1 = Image.FromFile(@"..\..\image\MB_ultimate1.png"); public Image imgBullet2 = Image.FromFile(@"..\..\image\MB_ultimate2.png"); //爆炸的imge集合 public Image[] imgExplode = { Image.FromFile(@"..\..\image\explode1.png"), Image.FromFile(@"..\..\image\explode2.png"), Image.FromFile(@"..\..\image\explode3.png"), Image.FromFile(@"..\..\image\explode4.png") }; //补给对象的img public Image imgSupply = Image.FromFile(@"..\..\image\bulletbox1.png"); //定义音乐对象 public Sound soundBackGround = new Sound(@"..\..\music\zengjia.mp3"); public Sound soundPrepare = new Sound(@"..\..\music\gamebegin.mp3"); public Sound soundMyBullt = new Sound(@"..\..\music\mybullt.mp3"); public Sound soundEnemyBullt = new Sound(@"..\..\music\bullt.mp3"); public Sound soundExplode = new Sound(@"..\..\music\missileexplode.mp3"); public Sound soundOver = new Sound(@"..\..\music\gameover.mp3"); //随机数 public Random rnd = new Random(); //定义游戏状态类 public GameState gameState = GameState.Prepare; //定义背景类 public BackGround background; //定义我军飞机类 public MyPlane myplane; //定义敌机对象集合 public List<EnemyPlane> listEnemyPlane=new List<EnemyPlane>(); //定义子弹对象列表 public List<Bullet> listBullt = new List<Bullet>(); //定义爆炸对象列表 public List<Explode> listExplode = new List<Explode>(); //定义补给对象列表 public List<Supply> listSupply = new List<Supply>(); //定义生命值 public StateBar stateBar; //构造函数 public gameconser() { InitializeComponent(); this.Text = "飞机大战"; this.BackColor = Color.Black; this.Width = 1000; this.Height = 500; this.StartPosition = FormStartPosition.CenterScreen;//指定窗口初始位置 this.MaximizeBox = false;//最大化 this.MinimizeBox = false;//最小化 this.FormBorderStyle = FormBorderStyle.FixedDialog;//指定边框样式 this.DoubleBuffered = true;//双缓冲技术:使用双缓冲技术绘制窗体,解决闪烁问题 background = new BackGround(0, 0, 1000, 500, 1, this); myplane = new MyPlane(10, 200, 80, 30, 10, 100, true, this); stateBar = new StateBar(30, 20, 50, 15, true, this); } //设置子线程线程 private void gameconser_Load(object sender, EventArgs e) { //Thread t = new Thread(new ThreadStart(Run));//创建子线程 //t.IsBackground = true;//设置为后台线程 //t.Start();//开始线程 } //画图 protected override void OnPaint(PaintEventArgs e) { base.OnPaint(e);//调用基类的Onpaint方法 if(gameState==GameState.Prepare)//y游戏准备状态 { e.Graphics.DrawImage(imgGamePrepare, 0, 0, 1000, 500); soundPrepare.RepeatPlay(); return; } if (gameState == GameState.Fail)//y游戏结束状态 { Thread.Sleep(1000);//暂停一秒 e.Graphics.DrawImage(imgGameOver, 0, 0, 1000, 500); soundOver.RepeatPlay(); return; } background.DrawMe(e.Graphics);//调用背景的DrawMe myplane.DrawMe(e.Graphics);//调用我军飞机的DrawMe stateBar.DrawMe(e.Graphics);//画出生命值 for(int i=0;i<listEnemyPlane.Count;i )//根据集合内的敌机数逐个调用DrawMe { listEnemyPlane[i].DrawMe(e.Graphics); } for (int i = 0; i < listBullt.Count; i )//画子弹 { listBullt[i].DrawMe(e.Graphics); } for (int i = 0; i < listExplode.Count; i )//画爆炸效果图 { listExplode[i].DrawMe(e.Graphics); soundExplode.AsyncPlay(); } for (int i = 0; i < listSupply.Count; i )//画补给图 { listSupply[i].DrawMe(e.Graphics); } } //每33毫秒刷新 public void Run() { while (true) { Thread.Sleep(33);//让当前线程休眠33毫秒 GreatEnemyPlane();//创建敌机 GreatSupply();//创建补给 this.Invalidate();//刷新窗体(使窗体重绘) } } //按键按下 private void gameconser_KeyDown(object sender, KeyEventArgs e) { if (e.KeyCode == Keys.Enter && gameState == GameState.Prepare)//enter键按下开始 { gameState = GameState.Start; Thread t = new Thread(new ThreadStart(Run));//创建子线程 t.IsBackground = true;//设置为后台线程 t.Start();//开始线程 soundBackGround.RepeatPlay(); } myplane.KeyDown(e); } //按键松开 private void gameconser_KeyUp(object sender, KeyEventArgs e) { myplane.KeyUp(e); } //根据随机数创建敌机 public void GreatEnemyPlane() { if(rnd.Next(100)%46==0)//百分之二创建从右方出来的一型敌机 { listEnemyPlane.Add(new EnemyPlane(1000, rnd.Next(450), 60, 20, rnd.Next(4, 10), 1, true, Direction.Left, this)); } if (rnd.Next(100) % 46 == 0)//百分之二创建从右方出来的二型敌机 { listEnemyPlane.Add(new EnemyPlane(1000, rnd.Next(450), 60, 20, rnd.Next(4, 10), 2, true, Direction.Left, this)); } if (rnd.Next(1000) % 580 == 0)//千分之一创建从下方出来的一型敌机 { listEnemyPlane.Add(new EnemyPlane(rnd.Next(300, 900), 500, 60, 20, rnd.Next(2, 5), 1, true, Direction.Up, this)); } if (rnd.Next(1000) % 580 == 0)//千分之一创建从下方出来而且向左上方移动的二型敌机 { listEnemyPlane.Add(new EnemyPlane(rnd.Next(600, 900), 500, 60, 20, rnd.Next(2, 5), 2, true, Direction.UpLeft, this)); } } //随机创建补给 public void GreatSupply() { if (rnd.Next(100) % 56 == 0) { listSupply.Add(new Supply(rnd.Next(100, 700), 0, 10, 30, rnd.Next(2, 5), true, Direction.Down, this)); } } }}

评论

发表评论必须先登陆, 您可以 登陆 或者 注册新账号 !


在线咨询: 问题反馈
客服QQ:174666394

有问题请留言,看到后及时答复